package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.Category;
import com.zrrd.reggie.bean.Dish;
import com.zrrd.reggie.bean.DishFlavor;
import com.zrrd.reggie.bean.dto.DishDto;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.service.CategoryService;
import com.zrrd.reggie.service.DishFlavorService;
import com.zrrd.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        Page<Dish> pageInfo=new Page<>(page,pageSize);
        QueryWrapper<Dish> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        if (name!=null && !name.equals("")){
            wrapper.like("name",name);
        }
        dishService.page(pageInfo,wrapper);
        Page<DishDto> dtoPage=new Page<>();
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Dish> dishList = pageInfo.getRecords();
        List<DishDto> dtoList = new ArrayList<>();
        for (Dish dish : dishList) {
            DishDto dto=new DishDto();
            BeanUtils.copyProperties(dish,dto);
            Category category = categoryService.getById(dish.getCategoryId());
            if (category!=null){
                dto.setCategoryName(category.getName());
            }
            dtoList.add(dto);
        }
        dtoPage.setRecords(dtoList);
        return R.sucess(dtoPage);
    }
    @PostMapping
    @CacheEvict(value = "dishCache",allEntries = true)
    public  R<String> save(@RequestBody DishDto dto){
            dishService.saveDish(dto);
            redisTemplate.delete("dish_"+dto.getCategoryId()+"_"+dto.getStatus());
            return  R.sucess("添加菜品成功");
    }
    @GetMapping("/{id}")
    public R<DishDto> getDishWithFlavors(@PathVariable("id") Long id){
        return R.sucess(dishService.getDishWithFlavors(id));
    }
    @PutMapping
    @CacheEvict(value = "dishCache",allEntries = true)
    public R<String> updateDishWithFlavors(@RequestBody DishDto dto){
        dishService.updateDishWithFlavors(dto);
        redisTemplate.delete("dish_"+dto.getCategoryId()+"_"+dto.getStatus());
        return R.sucess("修改菜品成功");
    }
    @PostMapping("/status/{status}")
    @CacheEvict(value = "dishCache",allEntries = true)
    public R<String> updateStatus(@PathVariable("status") Integer status,Long[] ids){
        List<Dish> dishList=new ArrayList<>();
        for (Long id : ids) {
            Dish dish=new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishList.add(dish);
        }
        dishService.updateBatchById(dishList);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return R.sucess("修改成功");
    }
    @DeleteMapping
    @CacheEvict(value = "dishCache",allEntries = true)
    public R<String> deleteDish(Long[] ids){
        dishService.removeByIds(Arrays.asList(ids));
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return R.sucess("删除成功");
    }
    @GetMapping("list")
    @Cacheable(value = "dishCache" ,key = "#dish.categoryId+'_'+#dish.status")
    public R<List> list(Dish dish){

        QueryWrapper<Dish> wrapper=new QueryWrapper<>();
        wrapper.eq("category_id",dish.getCategoryId());
        wrapper.eq("status",1);
        wrapper.orderByDesc("sort");
        List<Dish> dishList=dishService.list(wrapper);

        List<DishDto> dtoList=new ArrayList<>();
        for (Dish dishs : dishList) {
            DishDto dto=new DishDto();
            BeanUtils.copyProperties(dishs,dto);
            QueryWrapper<DishFlavor> dishFlavorQueryWrapper=new QueryWrapper<>();
            dishFlavorQueryWrapper.eq("dish_id",dishs.getId());
            List<DishFlavor> list = dishFlavorService.list(dishFlavorQueryWrapper);
            dto.setFlavors(list);
            dtoList.add(dto);
        }
        return R.sucess(dtoList);
    }
}
